package com.usts2022.classcostrecord;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.usts2022.classcostrecord.Utils.HttpUtils;
import com.usts2022.classcostrecord.entity.User;
import com.usts2022.classcostrecord.entity.UserViewModel;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MeFragment extends Fragment {

    private final static String TAG = "MeFragment";
    private ImageView myAvatar;
    private TextView mHint,myName,myClassCode;
    private String[] dataList;
    public static final int LOGIN_CODE = 100;
    private User user;
    private UserViewModel userViewModel;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        userViewModel = new ViewModelProvider(getActivity()).get(UserViewModel.class);
        user = userViewModel.getUser();
        Log.d(TAG, "userViewModel: " + userViewModel.getUser());


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        //绑定
        myAvatar = view.findViewById(R.id.my_avatar);
        mHint = view.findViewById(R.id.my_hint);
        myName = view.findViewById(R.id.my_name);
        myClassCode = view.findViewById(R.id.my_class_code);

        //读取登录信息
        if (user != null) {
            myClassCode.setText(" 所属班级:"+user.getClassCode());
            myName.setText(user.getName());
            mHint.setText("欢迎你!"+user.getName() + "!");
        }

        //头像点击事件
        myAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                getActivity().startActivityForResult(intent,LOGIN_CODE);
            }
        });
        return view;
    }



}